
part of game;

class TitanSprite extends Sprite
{
  
// 待機
FlumpMovie mIdle;
// 行走
FlumpMovie mWalk;
// 攻擊
FlumpMovie mJump;
// 擊倒
FlumpMovie mDefeat;


// 攻擊點
Shape mAttackShape;


DelayedCall mDelayCall;

bool mbDefeat;
bool mbEnableKill;

// 是否是奇行種
bool mbSpecial;
// 是否是鎧之肉人
bool mbSteel;
// 是否是超大型肉人
bool mbGiant;

  TitanSprite(double scale)
  {
    
    mbSpecial = false;
    mbSteel = false;
    mbGiant = false;
    

    // 攻擊點
    mAttackShape = new Shape();
    mAttackShape.graphics.beginPath();
    mAttackShape.graphics.circle(0, 0, 40);
    mAttackShape.graphics.closePath();
    mAttackShape.graphics.fillColor(0x55FF0000);  // ARGB
    mAttackShape.x = -10;
    mAttackShape.y = -140;

    
//    Sprite hitSprite = new Sprite();
   

    
    
    var textField = new TextField();
    textField.defaultTextFormat = new TextFormat('Helvetica,Arial', 36, Color.Black, bold:true, align:TextFormatAlign.CENTER);
    textField.width = 300;
    textField.height = 200;
    textField.x = -120;
    textField.y = -180 - 10 * scale;
    
    String name = null;
    if(scale>=0 && scale < 0.3)
    {
      name = "通常種3尺肉人";
    }
    else if(scale>=0.3 && scale < 0.6)
    {
      name = "通常種7公尺肉人";
    }
    else if(scale>=0.6 && scale < 1.0)
    {
      name = "通常種15公尺肉人";
    }
    // 奇形種
    else if(scale==1.0)
    {
      name = "奇行種肉人";
      mbSpecial = true;
    }
    // 鎧之肉人
    else if(scale==1.2)
    {
      name = "鎧之肉人";
      mbSteel = true;
      
      mAttackShape.graphics.fillColor(0x550000FF);
    }
    // 超大型肉人
    else if(scale==2.0)
    {
      name = "超大型肉人";
      mbGiant = true;
      
      mAttackShape.graphics.fillColor(0x550000FF);
    }
    
    textField.text = name;
    addChild(textField);
    
    // 建立動化物件
    mIdle = new FlumpMovie(gFlumpLibrary, "idle");
    mWalk = new FlumpMovie(gFlumpLibrary, "walk");
    mJump = new FlumpMovie(gFlumpLibrary, "attack");
    mDefeat = new FlumpMovie(gFlumpLibrary, "defeat");
     
    
    
    this.addEventListener(MouseEvent.CLICK, (MouseEvent me)
        {
      if(!mbEnableKill)
        return;
      
      // 巨人需要砍三次
      if(mbGiant)
      {
        mbGiant = false;
        mbSteel = true;
        return;
      }
      
      // 鎧之肉人要砍兩次
      if(mbSteel)
      {
        mAttackShape.graphics.fillColor(0x55FF0000);  // ARGB
        mbSteel = false;
        return;
      }
      
      mbEnableKill = false;
      print("砍巨人");
      defeat();

        });    
       
    
    mbDefeat = false;
    mbEnableKill = false;
    
    idle();
    
  }
  
  
  
 // The Juggler Framework
 // 1. All classes which implements the Animatable interface can be added to the Juggler. 
 // 2. The Juggler controls the timeline.
 // 所以每個動畫物件都必須加入Juggler 他和Node是分開的
  
  // Animatable
  // The boolean return value indicates if the Animatable is still active.
  // If 'false' is returned, the Animatable will be automatically removed
  // from the Juggler and therefore it will no longer be called. 
  
// 待機
  void idle()
  {
    print("巨人待機");     
        
    if(mbDefeat)
      return;
    
    mbEnableKill = true;
    
    // 這樣真的可行嗎？
    gRenderLoop.juggler.removeTweens(this);
    mAttackShape.removeFromParent();
    
    if(mbSpecial)
    {
      gRenderLoop.juggler.remove(mJump);
      mJump.removeFromParent();
    }
    else
    {
      gRenderLoop.juggler.remove(mWalk);
      mWalk.removeFromParent();
    }


    

    gRenderLoop.juggler.add(mIdle);
    addChild(mIdle);
    
    mIdle.scaleX = -1;
    
    
    addChild(mAttackShape);    


    // 不會遲疑太久（呆個三秒 又繼續前進）
    if(!gbStopCreate)
    {
      if(mbSpecial)
      {
        gRenderLoop.juggler.delayCall(jump, 1.0);    
      }
      else
      {
        gRenderLoop.juggler.delayCall(walk, 1.0);
      }
    }

  }
 
// 行走
void walk()
{  
//    int len = nuts.length;
//    for(int i=0; i<len; i++)
//    {
//      if(_hitNuts(nuts.elementAt(i)))
//      {
//        // 改為攻擊
//        attack(mCurrent.x, mCurrent.y);
//        return;
//      }
//    } 
  
  print("巨人前進");
  
  if(mbDefeat)
    return;  
  
  mbEnableKill = false;
  

  gRenderLoop.juggler.removeTweens(this);  
  gRenderLoop.juggler.remove(mIdle);
  mAttackShape.removeFromParent();   
  mIdle.removeFromParent();  
 

  gRenderLoop.juggler.add(mWalk);  
  addChild(mWalk);  

  // 轉為向右
  mWalk.scaleX = -1;
// 只是看不到 作用範圍還在  
//  addChild(mAttackShape);     
  
  
  // 越遠需要越快
   var time = 0.5 + (900 - x)/100;
  
  
  var tween = new Tween(this,  time, TransitionFunction.linear); // 10.0
  tween.animate.x.to(900.0);
  tween.onUpdate = () {
  };
  
  // 成功移動到畫面外面了
  tween.onComplete = () {   
    
    print("巨人完成攻城");        
    
    gbStopCreate = true;
//    destory();
    
    
    // GameOver
    gbGameState = 3;
    
    // 恢復按鈕
    gStage.addChild(gFailed);
    gStage.addChild(gStartButton);
    gStage.addChild(gStartButtonTextField);    
    gStartButtonTextField.text = "End";
    
    gStage.addChild(gTitanDefeatTextField);
    gTitanDefeatTextField.text = gDefeatCount.toString() + "隻擊倒";
    
    
    
    gStage.addChild(gTitleTextField);
 
    
    if(gDefeatCount < 20)
    {
      gTitleTextField.text = "訓練兵團畢業生";   
    }
    else if(gDefeatCount >= 20 && gDefeatCount < 40)
    {
      gTitleTextField.text = "調查軍團士兵";         
    }
    else if(gDefeatCount >= 20 && gDefeatCount < 40)
    {
      gTitleTextField.text = "調查軍團班長";         
    }
    else if(gDefeatCount >= 40 && gDefeatCount < 55)
    {
      gTitleTextField.text = "調查軍團副隊長";
    }
    else if(gDefeatCount >= 55 && gDefeatCount < 70)
    {
      gTitleTextField.text = "調查軍團隊長";         
    }
    else if(gDefeatCount >= 70 && gDefeatCount < 85)
    {
      gTitleTextField.text = "調查軍團兵長";         
    }    
    else if(gDefeatCount >= 85 && gDefeatCount < 100)
    {
      gTitleTextField.text = "調查軍團副團長";         
    } 
    

    
  };

  gRenderLoop.juggler.add(tween);
  
//  if(!tween.isComplete)
//  {
//  if(!gbStopCreate)
  gRenderLoop.juggler.delayCall(idle, 1.0 + 2.0 * gRandom.nextDouble());
//  }
  

}


// 跳
void jump()
{  
//    int len = nuts.length;
//    for(int i=0; i<len; i++)
//    {
//      if(_hitNuts(nuts.elementAt(i)))
//      {
//        // 改為攻擊
//        attack(mCurrent.x, mCurrent.y);
//        return;
//      }
//    } 
  
  print("奇形種前進");
  
  if(mbDefeat)
    return;  
  
  mbEnableKill = false;
  

  gRenderLoop.juggler.removeTweens(this);  
  gRenderLoop.juggler.remove(mIdle);
  mAttackShape.removeFromParent();   
  mIdle.removeFromParent();  
 

  gRenderLoop.juggler.add(mJump);  
  addChild(mJump);  

  // 轉為向右
  mJump.scaleX = -1;
// 只是看不到 作用範圍還在  
//  addChild(mAttackShape);     
  
  
  // 越遠需要越快
   var time = 0.2 + (900 - x)/50;
  
  
  var tween = new Tween(this,  time, TransitionFunction.easeOutQuintic); // 10.0
  tween.animate.x.to(900.0);
  tween.onUpdate = () {
  };
  
  // 成功移動到畫面外面了
  tween.onComplete = () {   
    
    print("巨人完成攻城");        
    
    gbStopCreate = true;
//    destory();
    
    
    // GameOver
    gbGameState = 3;
    
    // 恢復按鈕
    gStage.addChild(gFailed);
    gStage.addChild(gStartButton);
    gStage.addChild(gStartButtonTextField);    
    gStartButtonTextField.text = "End";

    
  };

  gRenderLoop.juggler.add(tween);
  
//  if(!tween.isComplete)
//  {
  
  // 奇形種 不太會休息
  if(!gbStopCreate)
  gRenderLoop.juggler.delayCall(idle, 1.0 + 2.0 * gRandom.nextDouble());
//  }
  

}


// 打敗
void defeat()
{
  print("巨人打敗");     
  

  
//  播放聲音
  SoundTransform soundTransform = new SoundTransform(1);
  Sound sound = gResourceManager.getSound("plop");
  SoundChannel soundChannel = sound.play(false, soundTransform);  
  
  mbDefeat = true;
  
 
    gRenderLoop.juggler.removeTweens(this);
    gRenderLoop.juggler.remove(mIdle);    
    gRenderLoop.juggler.remove(mWalk);
    mAttackShape.removeFromParent();  
    mIdle.removeFromParent();
    mWalk.removeFromParent();
  
  
  gRenderLoop.juggler.add(mDefeat);
  addChild(mDefeat);
  
  mDefeat.scaleX = -1;
  
 
  gRenderLoop.juggler.delayCall(destory, 0.6);    

}


void destory()
{
  print("巨人回收");
  
  gDefeatCount++;  
  // 改變數值顯示
  gTitanCountTextField.text = gDefeatCount.toString()+"/100";
  
  gRenderLoop.juggler.removeTweens(this);
  gRenderLoop.juggler.remove(mDefeat);
  this.removeFromParent();
  
  if(gDefeatCount >= 100)
  {
    gbGameState = 2;
    
    gStage.addChild(gSuccess);
    gStage.addChild(gStartButton);
    gStage.addChild(gStartButtonTextField);    
    gStartButtonTextField.text = "End";
    
     print("遊戲達成...");
     
     
     gStage.addChild(gTitleTextField);
     gTitanDefeatTextField.text = "100隻擊倒";
     
     
     gStage.addChild(gTitleTextField);
     gTitleTextField.text = "調查軍團團長";
     
     
  }
  
}

}



